package com.spddr.pmos.rpt.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.joda.time.Minutes;
import org.joda.time.Period;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.spddr.pmos.rpt.RptPlantPlan;
import com.spddr.pmos.rpt.pojo.CellValue;

import cn.seqdata.echarts.Option;
import cn.seqdata.echarts.axis.CategoryAxis;
import cn.seqdata.echarts.axis.ValueAxis;
import cn.seqdata.echarts.series.Line;
import cn.seqdata.tags.Point;
import cn.seqdata.tags.common.IdTags;

@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class RptPlantPlanImpl extends AbstractPivotChartImpl implements RptPlantPlan {

	private List<String> metrics = Lists.newArrayList("发电能力", "电网计划", "上网负荷");

	protected Period intervalOfStorage() {
		return Minutes.minutes(15).toPeriod();
	}

	@Override
	public List<CellValue> loadPivot() {
		return sim(15).getValue().getValues().stream()
				.map(v -> new CellValue(new Point<>(null, new IdTags(id)), v, null, null)).collect(Collectors.toList());
	}

	@Override
	public Option chart() {
		Option option = new Option();

		CategoryAxis xAxis = new CategoryAxis().show(true);
		xAxis.setFunc(String.format("Admin.Array.minutes(15, 'H:i', '%1$s', '%2$s')", getStartTime(), getEndTime()));
		option.xAxis(xAxis);

		option.yAxis(new ValueAxis().show(true));

		metrics.stream().forEach(x -> {
			option.legend().show(true).data().add(x);

			Line line = new Line(x);
			sim(15).getValue().getValues().forEach(v -> {
				line.data().add(String.format("%.2f", v.getValue()));
			});
			option.series().add(line);
		});

		return option;
	}

}
